<?php
namespace app\tools\logic;
use think\facade\Db;

class EmailLogic{
	static function getTypeList(){
		$page_size = input('page_size', 10);
		$sqls1 = "select email_id, count(id) as nums from dg_email_list where status=1 and has_buy=0 GROUP BY email_id";
		$email_name = input('email_name', '');
		$obj = Db::name('email')
					->alias('e')
					->field('e.*, IFNULL(s.nums,0) as nums')
					->join("($sqls1) s","s.email_id=e.id",'left')
					->where('e.status','<>',127);
		$email_name && $obj->where('e.email_name','like','%'.$email_name.'%');
		$objs = $obj->paginate($page_size);

		$objs->render();
    	$list = $objs->toArray();
		if($list && $list['data']){
			foreach($list['data'] as $k=>$v){
				$list['data'][$k]['pack'] = Db::name('email_pack')
									->where('email_id', $v['id'])
									->where('status','<>',127)
									->select()->toArray();
			}
		}
		return $list;
	}

	

	static function getTypeInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('email')
					->where('id', $id)
					->find();
		$pack = Db::name('email_pack')
				->where('email_id', $id)
				->where('status','<>',127)
				->order('id asc')
				->select();
		$info['pack'] = $pack;
		return $info;

	}

	static function saveType(){
		$id = input('id');
		if(!$id){
			return 110;
		}
		$data = [];
		$data['email_name'] = input('email_name','');
		$data['email_name_en'] = input('email_name_en','');
		$data['status'] = input('status', 1);

		$pack = input('pack', []);

		// 启动事务
		Db::startTrans();
		try {
		    Db::name('email')->where('id', $id)->update($data);
		    //先全部删除
		    Db::name('email_pack')
		    	->where('email_id', $id)
		    	->update(['status'=>127]);
		    if($pack){
		    	foreach($pack as $k=>$v){
		    		//查询是否已经存在了
		    		$tD = [];
		    		$tD['email_id'] = $id;
		    		$tD['number'] = $v['number'];
		    		$tD['price'] = $v['price'];
		    		$tD['status'] = 1;
		    		if(isset($v['id']) && $v['id']){
		    			Db::name('email_pack')->where('id', $v['id'])->update($tD);
		    		}else{
		    			$tD['create_time'] = date('Y-m-d H:i:s');
		    			Db::name('email_pack')->insert($tD);
		    		}
		    		
		    	}
		    }
		    // 提交事务
		    Db::commit();
		    return 0;
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return 100000;
		}

	}

	static function addType(){
		$data = [];
		$data['email_name'] = input('email_name','');
		$data['email_name_en'] = input('email_name_en','');
		$data['status'] = input('status', 1);
		$data['create_time'] = date('Y-m-d H:i:s');

		$pack = input('pack', []);

		// 启动事务
		Db::startTrans();
		try {
		    $id = Db::name('email')->insertGetId($data);
		    if($pack){
		    	foreach($pack as $k=>$v){
		    		$tD = [];
		    		$tD['email_id'] = $id;
		    		$tD['number'] = $v['number'];
		    		$tD['price'] = $v['price'];
		    		$tD['status'] = 1;
		    		$tD['create_time'] = date('Y-m-d H:i:s');
		    		Db::name('email_pack')->insert($tD);
		    	}
		    }
		    // 提交事务
		    Db::commit();
		    return 0;
		} catch (\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return 100000;
		}
	}

	static function deleteType(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('email')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	#----套餐----
	// static function packList(){
	// 	$email_id = input('email_id', 0);
	// 	$list = Db::name('email_pack')
	// 			->where('email_id', $email_id)
	// 			->where('status','<>',127)
	// 			->paginate(10);
	// 	return $list;
	// }
	// static function packGetInfo(){
	// 	$id = input('id');
	// 	if(!$id){
	// 		return '';
	// 	}
	// 	$info = Db::name('email_pack')
	// 				->where('id', $id)
	// 				->find();
	// 	return $info;
	// }
	// static function packAdd(){
	// 	$data = [];
	// 	$data['email_id'] = input('email_id', 0);
	// 	if(!$data['email_id']){
	// 		return 119;
	// 	}
	// 	$data['pack_name'] = input('pack_name','');
	// 	$data['number'] = input('number', 1);
	// 	$data['price'] = input('price', 1);
	// 	$data['status'] = input('status', 1);
	// 	$data['create_time'] = date('Y-m-d H:i:s');

	// 	$rs = Db::name('email_pack')->insert($data);
	// 	if($rs!==false){
	// 		return 0;
	// 	}else{
	// 		return 100000;
	// 	}
	// }
	// static function packSave(){
	// 	$data = [];
	// 	$id = input('id', 0);
	// 	if(!$id){
	// 		return 110;
	// 	}
	// 	$data['email_id'] = input('email_id', 0);
	// 	if(!$data['email_id']){
	// 		return 119;
	// 	}
	// 	$data['pack_name'] = input('pack_name','');
	// 	$data['number'] = input('number', 1);
	// 	$data['price'] = input('price', 1);
	// 	$data['status'] = input('status', 1);
	// 	$rs = Db::name('email_pack')
	// 			->where('id', $id)
	// 			->update($data);
	// 	if($rs!==false){
	// 		return 0;
	// 	}else{
	// 		return 100000;
	// 	}
	// }

	// static function packDelete(){
	// 	$id = input('id','');
	// 	$ids = explode(',', $id);
	// 	if(!$ids){
	// 		return 110;
	// 	}
	// 	$rs = Db::name('email_pack')
	// 			->where('id','in', $ids)
	// 			->update(['status'=>127]);
	// 	if($rs!==false){
	// 		return 0;
	// 	}else{
	// 		return 100000;
	// 	}
	// }
	#----套餐----


	static function getList(){
		$email_id = input('email_id', 0);
		$email_account = input('email_account', '');
		$page_size = input('page_size', 10);
		$has_buy = input('has_buy', 0);
		if($email_account){
			$list = Db::name('email_list')
				->where('email_id', $email_id)
				->where('email_account','like','%'.$email_account.'%')
				->where('has_buy', $has_buy)
				->order('id desc')
				->paginate($page_size);
		}else{
			$list = Db::name('email_list')
				->where('email_id', $email_id)
				->where('has_buy', $has_buy)
				->order('id desc')
				->paginate($page_size);
		}
				
		return $list;
	}


	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('email_list')
					->where('id', $id)
					->find();
		return $info;

	}

	static function save(){
		$id = input('id');
		if(!$id){
			return 110;
		}
		$data = [];
		$data['email_id'] = input('email_id',0);
		if(!$data['email_id']){
			return 119;
		}
		$data['email_account'] = input('email_account', '');
		$data['email_password'] = input('email_password', '');
		$data['status'] = input('status', 1);

		$rs = Db::name('email_list')
				->where('id', $id)
				->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function add(){
		$data = [];
		$data['email_id'] = input('email_id',0);
		if(!$data['email_id']){
			return 119;
		}
		$data['email_account'] = input('email_account', '');
		$data['email_password'] = input('email_password', '');
		$data['is_lock'] = 0;
		$data['status'] = input('status', 1);
		$data['create_time'] = date('Y-m-d H:i:s');

		$rs = Db::name('email_list')->insert($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('email_list')
				->where('id','in', $ids)
				->delete();
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function addList(){
		$email_id = input('email_id', 0);
		if(!$email_id){
			return ['code'=>119];
		}

		$item = input('item', []);
		if(!$item){
			return ['code'=>126];
		}
		$cf = 0;
		$cg = 0;
		foreach($item as $k=>$v){
			$v = trim($v,'-');
			$acc = explode('-', $v);
			$data = [];
			$data['email_id'] = $email_id;
			$data['email_account'] = $acc[0];
			$data['email_password'] = $acc[count($acc)-1];
			$data['create_time'] = date('Y-m-d H:i:s');
			$info = Db::name('email_list')
					->where('email_account', $data['email_account'])
					->find();
			if($info){
				$cf++;
				continue;
			}
			$rs = Db::name('email_list')->insert($data);
			$cg++;
		}
		return ['code'=>0,'cf'=>$cf,'cg'=>$cg];
	}

	static function deleteAll(){
		$email_id = input('email_id', 0);
		if(!$email_id){
			return 119;
		}
		$rs = Db::name('email_list')
				->where('email_id', $email_id)
				->delete();
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}
}